package com.example.demo.service;

import com.example.demo.bean.Status;
import com.example.demo.mapper.CarMapper;
import com.example.demo.mapper.MessageMapper;
import com.example.demo.mapper.Message_OwnerMapper;
import com.example.demo.mapper.StatusMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Random;

@Service
public class StatusChangeService {
    @Resource
    CarMapper carMapper;
    @Resource
    MessageMapper messageMapper;
    @Resource
    Message_OwnerMapper messageOwnerMapper;
    @Resource
    StatusMapper statusMapper;
    @Resource
    CarChangeService carChangeService;

    public List<Integer> change_statuses(int car_id,int message_id,int is_end){
        int status_id = Integer.parseInt(carMapper.getCarStatus(car_id).get("status_id").toString());
        List<Integer> statuses = new ArrayList<>();
        changeStatus(car_id);
        if(status_id == 5){
            for (int i = 0 ; i < 2 ;i++ ){
                changeStatus(car_id);
                statuses.add(Integer.parseInt(carMapper.getCarStatus(car_id).get("status_id").toString()));
            }
            statuses.add(1);
            carMapper.changeStatus(1,car_id);
            carMapper.addIndustry(messageMapper.get_industry(message_id), car_id);
        }
        else {
            if(status_id == 1 && is_end == 1){
                for (int i = 0 ; i < 2 ;i++ ){
                    changeStatus(car_id);
                    statuses.add(Integer.parseInt(carMapper.getCarStatus(car_id).get("status_id").toString()));
                }
                statuses.add(3);
                carChangeService.carChange(message_id,car_id);
            }
            if(status_id == 1 && is_end == 0){
                for (int i = 0 ; i < 2 ;i++ ){
                    changeStatus(car_id);
                    statuses.add(Integer.parseInt(carMapper.getCarStatus(car_id).get("status_id").toString()));
                }
                statuses.add(3);
                carMapper.changeStatus(1,car_id);
            }
//            else {
//                carMapper.changeStatus(5,car_id);
//                statuses.add(Integer.parseInt(carMapper.getCarStatus(car_id).get("status_id").toString()));
//            }
        }
        return statuses;
    }
    public void changeStatus(int car_id){
        Random random = new Random();
        List<Status> statuses = statusMapper.getMtab();
        int status_id = Integer.parseInt(carMapper.getCarStatus(car_id).get("status_id").toString());
        switch (status_id){
            /*0 : 接单
             *1 : 装货
             *2 : 运货
             *3 : 卸货
             *4 : 接单车辆保养
             *5 : 空闲
             *6 : 接单车辆故障
             *7 : 运货车辆保养
             *8 : 运货车辆故障
             */
            case 5:{
                carMapper.changeStatus(0,car_id);
                break;
            }
            case 0:{
                double p = random.nextDouble();
                if (p <= statuses.get(0).getNext_status0()) {
                    carMapper.changeStatus(0, car_id);
//                    carMapper.addGoods(messageMapper.get_goods(message_id), car_id);
                }
                else {
                    if(p <= statuses.get(0).getNext_status1() + statuses.get(0).getNext_status4()){
                        carMapper.changeStatus(4,car_id);
                    }
                    else {
                        carMapper.changeStatus(6, car_id);
                    }
                }
                break;
            }
            case 1:{
                carMapper.changeStatus(2,car_id);
                break;
            }
            case 2:{
                double p = random.nextDouble();
                if (p <= statuses.get(2).getNext_status2()) {
                    carMapper.changeStatus(2, car_id);
//                    carMapper.delGoods(car_id);
                }
                else {
                    if (p <= statuses.get(2).getNext_status3() + statuses.get(2).getNext_status7()){
                        carMapper.changeStatus(7,car_id);
                    }
                    else {
                        carMapper.changeStatus(8,car_id);
                    }
                }
                break;
            }
            case 3:{
//                carMapper.releaseCar(car_id);
                carMapper.changeStatus(5,car_id);
                break;
            }
            case 4:{
                double p = random.nextDouble();
                if (p <= statuses.get(4).getNext_status0()){
                    carMapper.changeStatus(0,car_id);
                }
                else {
                    carMapper.changeStatus(4,car_id);
                }
                break;
            }
            case 6:{
                double p = random.nextDouble();
                if (p <= statuses.get(6).getNext_status0()){
                    carMapper.changeStatus(0,car_id);
                }
                else {
                    if (p <= statuses.get(6).getNext_status0() + statuses.get(6).getNext_status4()){
                        carMapper.changeStatus(4,car_id);
                    }
                    else {
                        carMapper.changeStatus(6,car_id);
                    }
                }
                break;
            }
            case 7:{
                double p = random.nextDouble();
                if (p <= statuses.get(7).getNext_status2()){
                    carMapper.changeStatus(2,car_id);
                }
                else {
                    carMapper.changeStatus(7,car_id);
                }
                break;
            }
            case 8:{
                double p = random.nextDouble();
                if (p <= statuses.get(8).getNext_status2()){
                    carMapper.changeStatus(2,car_id);
                }
                else {
                    if (p <= statuses.get(8).getNext_status2() + statuses.get(8).getNext_status7()){
                        carMapper.changeStatus(7,car_id);
                    }
                    else {
                        carMapper.changeStatus(8,car_id);
                    }
                }
                break;
            }
        }
    }

    public boolean checkMessage(int message_id){
        return carMapper.getCar_id_num(message_id) == 0;
    }

    public int getStatus(int car_id){
        return Integer.parseInt(carMapper.getCarStatus(car_id).get("status_id").toString());
    }

    public void setMessageDone(int message_id){
        messageMapper.setMessage(message_id);
    }

    public void carPosition_change(int message_id,int car_id){
        int end_id = messageMapper.select_end(message_id);
        carMapper.changeCarPosition(car_id,end_id);
    }

    public void carChange(int message_id,int car_id){
        carMapper.lock(car_id);
        carMapper.changeStatus(5,car_id);
        carMapper.delIndustry(car_id);
        carMapper.releaseCar(car_id);
        carPosition_change(message_id,car_id);
    }
}
